<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue'

const emit = defineEmits<{
  loaded: []
}>()

const loadingText = ref('正在初始化...')
let dataReady = false
let minTimeElapsed = false

// 检查是否可以完成加载
const checkCanFinish = () => {
  if (dataReady && minTimeElapsed) {
    console.log('✅ 启动画面：数据和最小时间都已就绪，触发loaded事件')
    emit('loaded')
  }
}

// 监听首页数据加载完成事件
const handleHomeDataReady = () => {
  console.log('✅ 启动画面：收到首页数据就绪事件')
  dataReady = true
  loadingText.value = '加载完成...'
  checkCanFinish()
}

// 立即添加事件监听器（在setup时就执行，确保不会错过事件）
console.log('🎬 启动画面：setup阶段，立即添加事件监听')
window.addEventListener('home-data-ready', handleHomeDataReady)

onMounted(() => {
  console.log('🎬 启动画面：onMounted开始显示')
  
  // 最少显示1.5秒的动画
  setTimeout(() => {
    console.log('🎬 启动画面：最小时间已到')
    minTimeElapsed = true
    checkCanFinish()
  }, 1500)
  
  // 超时保护：5秒后强制进入（防止数据加载失败卡住）
  setTimeout(() => {
    if (!dataReady) {
      console.warn('⚠️ 启动画面：数据加载超时，强制进入')
      emit('loaded')
    }
  }, 5000)
})

onUnmounted(() => {
  console.log('🎬 启动画面：清理事件监听')
  window.removeEventListener('home-data-ready', handleHomeDataReady)
})
</script>

<template>
  <div class="splash-screen">
    <div class="splash-content">
      <!-- 大的人民币符号 -->
      <div class="yen-symbol">¥</div>
      
      <h1 class="app-title">记账本</h1>
      <p class="app-subtitle">智能记账，轻松理财</p>
      <p class="loading-text">{{ loadingText }}</p>
      
      <div class="loading-dots">
        <span></span>
        <span></span>
        <span></span>
      </div>
    </div>
  </div>
</template>

<style scoped>
.splash-screen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #ff6bb5 0%, #ff8fd4 25%, #d896ff 75%, #b57bee 100%);
  background-size: 400% 400%;
  animation: gradientShift 3s ease infinite;
}

@keyframes gradientShift {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.splash-content {
  text-align: center;
  color: white;
}

.yen-symbol {
  font-size: 120px;
  font-weight: bold;
  margin: 0 0 30px 0;
  animation: float 2s ease-in-out infinite, rotate 3s linear infinite;
  text-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) scale(1);
  }
  50% {
    transform: translateY(-20px) scale(1.05);
  }
}

@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.app-title {
  font-size: 40px;
  font-weight: bold;
  margin: 0 0 12px 0;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  animation: fadeInUp 0.8s ease-out;
}

.app-subtitle {
  font-size: 18px;
  margin: 0 0 20px 0;
  opacity: 0.9;
  animation: fadeInUp 0.8s ease-out 0.2s both;
}

.loading-text {
  font-size: 16px;
  margin: 0 0 20px 0;
  opacity: 0.8;
  animation: fadeInUp 0.8s ease-out 0.3s both;
  font-weight: 500;
}

.loading-dots {
  display: flex;
  justify-content: center;
  gap: 10px;
  animation: fadeInUp 0.8s ease-out 0.4s both;
}

.loading-dots span {
  width: 12px;
  height: 12px;
  background: white;
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out both;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.loading-dots span:nth-child(1) {
  animation-delay: -0.32s;
}

.loading-dots span:nth-child(2) {
  animation-delay: -0.16s;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes bounce {
  0%, 80%, 100% {
    transform: scale(0);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}
</style>
